package com.lzp.appexp.main

import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.base.Constants
import com.lzp.appexp.R
import com.net.NetCallBack
import com.net.NetUtils
import okhttp3.Response
import java.io.IOException

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/3/13
 */
class MainActivityNew : BaseMainActivity() {

    private val TAG = "MainActivityNew"
    companion object {
        const val INDEX = "index"

        fun startMainActivity(context: Context, index: Int = 0) {
            val intent = Intent(context, MainActivityNew::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            val bundle = Bundle()
            bundle.putInt(INDEX, index)
            intent.putExtra(Constants.EXTRA_BUNDLE, bundle)
            context.startActivity(intent)
        }
    }

    private var index = 0

    override fun readArgument(bundle: Bundle) {
        super.readArgument(bundle)
        index = bundle.getInt(INDEX, index)
    }

    override fun writeArgument(bundle: Bundle) {
        super.writeArgument(bundle)
        bundle.putInt(INDEX, index)
    }

    override fun initView() {
        super.initView()
        setSelectedIndex(index)
        onTabChange(-1, index)
        NetUtils.get().url("https://www.wanandroid.com/banner/json")
                .build().execute(object : NetCallBack {
                    override fun onSuccess(response: Response) {
                        Log.d(TAG, "onSuccess: ")
                    }

                    override fun onError(e: IOException) {
                        Log.d(TAG, "onError: ")
                    }
                })

        /*ImageLoader.get(this)
                .load("https://goss.veer.com/creative/vcg/veer/1600water/veer-375318408.jpg")
                .listener(object :ImgLoadListener.LoadListener{
                    override fun success(t: File?) {
                        Log.d("MainActivity",t?.absolutePath?:"")
                    }

                    override fun fail(e: Exception?) {
                        Log.d("MainActivity","fail+${e?.message}")
                    }
                })
                .start()*/

        val intent = Intent(this, TestService::class.java)
        bindService(intent, conn, Context.BIND_AUTO_CREATE)
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            startForegroundService(intent)
        }
    }

    private var startTime = System.currentTimeMillis()

    private val conn = object : ServiceConnection{
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            Log.d(TAG, "onServiceConnected:开始时间 ${(System.currentTimeMillis() - startTime) / 1000} 秒")
        }

        override fun onServiceDisconnected(name: ComponentName?) {
        }

    }
}